package yf.mall.dysdk.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yf.utils.HttpTookit;
import com.yf.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.HttpUrl;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.http.HttpResponse;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service;
import yf.mall.dysdk.config.DyConfig;
import yf.mall.dysdk.config.UrlConfig;
import yf.mall.dysdk.model.DyAuthCheckResponse;
import yf.mall.dysdk.model.DyAuthCodeResponse;
import yf.mall.dysdk.model.DyToken;
import yf.mall.dysdk.model.common.DyList;
import yf.mall.dysdk.model.user.DyFansData;
import yf.mall.dysdk.model.user.DyUserInfo;
import yf.mall.dysdk.model.common.DyResponse;
import yf.mall.dysdk.model.user.DyFullUserInfo;
import yf.mall.dysdk.model.user.UserResponse;
import yf.mall.dysdk.service.DyAuthService;
import yf.mall.dysdk.utils.DyUtil;
import yf.mall.dysdk.utils.HttpUtils;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static yf.mall.common.Const.TK_DOMAIN;

@Slf4j
@Service
public class DyAuthServiceImpl implements DyAuthService {

    @Resource
    private DyConfig config;




    @Override
    public DyToken getDyAccessToken(String code) {
        Map map = new HashMap();
        map.put("code",code);
        map.put("clientKey",config.getClientKey());
        String res = HttpTookit.doGet(TK_DOMAIN+"getDyAccessToken",map);
        return JSONObject.parseObject(res,DyToken.class);
    }

    @Override
    public DyToken refreshAccessToken(String refreshToken) {
        Map map = new HashMap();
        map.put("refreshToken",refreshToken);
        map.put("clientKey",config.getClientKey());
        String res = HttpTookit.doPost(TK_DOMAIN+"refreshAccessToken",map);
        return JSONObject.parseObject(res,DyToken.class);
    }

    @Override
    public DyToken refreshRefreshToken(String refreshToken) {
        Map map = new HashMap();
        map.put("refreshToken",refreshToken);
        map.put("clientKey",config.getClientKey());
        String res = HttpTookit.doPost(TK_DOMAIN+"refreshRefreshToken",map);
        return JSONObject.parseObject(res,DyToken.class);
    }

    @Override
    public DyToken getDyClientToken() {
        Map map = new HashMap();
        map.put("clientKey",config.getClientKey());
        String res = HttpTookit.doPost(TK_DOMAIN+"getDyClientToken",map);
        return JSONObject.parseObject(res,DyToken.class);
    }


    @Override
    public DyAuthCodeResponse oauthConnect(Long userId) {
        Map map = new HashMap();
        map.put("clientKey",config.getClientKey());
        map.put("userId",String.valueOf(userId));
        String res = HttpTookit.doPost(TK_DOMAIN+"oauthConnect",map);
        return JSONObject.parseObject(res,DyAuthCodeResponse.class);
    }

    @Override
    public DyAuthCheckResponse oauthQuery(Long userId, String token) {
        Map map = new HashMap();
        map.put("clientKey",config.getClientKey());
        map.put("token",token);
        map.put("userId",String.valueOf(userId));
        String res = HttpTookit.doPost(TK_DOMAIN+"oauthQuery",map);
        return JSONObject.parseObject(res,DyAuthCheckResponse.class);
    }

    @Override
    public DyUserInfo getUserInfo(DyToken dyToken) {
        String res = HttpTookit.doPostStr(TK_DOMAIN+"getUserInfo",JSONObject.toJSONString(dyToken));
        return JSONObject.parseObject(res,DyUserInfo.class);
    }



    @Override
    public DyFullUserInfo getDyFullUserInfo(String openId) {
        Map map = new HashMap();
        map.put("clientKey",config.getClientKey());
        map.put("openId",openId);
        String res = HttpTookit.doPost(TK_DOMAIN+"getDyFullUserInfo",map);
        return JSONObject.parseObject(res,DyFullUserInfo.class);
    }



}
